package com.wing.cms.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.cms.dao.ArtgroupDao;
import com.wing.cms.dao.Article_groupDao;
import com.wing.cms.domain.Artgroup;
import com.wing.cms.domain.Article_group;
import com.wing.common.Constants;
import com.wing.common.util.FileUtils;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Code;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class ArtgroupManager {
	@Autowired
	private ArtgroupDao artgroupdao;
	@Autowired
	private Article_groupDao article_groupDao;
	
	
	
	/**
	 * 获得该对象所有数据的列表
	 * 
	 * @return list
	 */
	public List<Map<String,Object>> findAll(){
		return artgroupdao.findAll();	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String pid, String siteId) {
		return artgroupdao.findPageList(currentPage,pageSize,pid,siteId);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public Artgroup findById(String id){
		return artgroupdao.findById(id);	 
	}
	
	/**
	 * 保存对象
	 *
	 * @param  artgroup
	 */
	public void saveArtgroup(Artgroup artgroup){
		if(artgroup.getId() == null ||  "".equals(artgroup.getId())){
			artgroup.setId(new GUID().toString());
			artgroupdao.insert(artgroup);
		}else{
			artgroupdao.update(artgroup);
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  artgroup
	 */
	public void updateArtgroup(Artgroup artgroup){
		artgroupdao.update(artgroup);
	}
	
	 /**
	 * 删除对象
	 *
	 * @param  chk 对象id数组
	 */
	public void deleteArtgroups(String id,HttpServletRequest request) {
		article_groupDao.deleteArticle_groupByGrpId(id);
		deleteArtgroup(id,request);
	}
	
	/**
	 * 递归删除其下面的子文章组
	 * @param artgroupId
	 */
	public void deleteArtgroup(String artgroupId,HttpServletRequest request){
		List<Map<String, Object>> list = artgroupdao.findChildByID(artgroupId);
		for(int i=0;i<list.size();i++){
			Map map = list.get(i);
			deleteArtgroup((String)map.get("id"),request);
		}
		//删除文章组和文章的关系
		article_groupDao.deleteArticle_groupByGrpId(artgroupId);
		//删除文章组下的物理文件
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		String grpPath = request.getSession().getServletContext().getRealPath("/")+artgroupId;
		FileUtils.deleteDir(grpPath);
		artgroupdao.delete(artgroupId);
	}

	public void addArtForGrp(String grpId, String artIds) {
		String ids[] = artIds.split("\\|");
		for(int i=0;i<ids.length;i++){
			if(ids[i].trim().equals("")){
				continue;
			}
			List list = article_groupDao.getArticle_groupByGrpIdArtId(grpId, ids[i]);
			if(list.size()==0){
				Article_group ag = new Article_group();
				ag.setArticle_id(ids[i]);
				ag.setArtgroup_id(grpId);
				ag.setId(new GUID().toString());
				article_groupDao.insert(ag);
			}
		}
	}
	
	public List<Map<String, Object>> findChildByID(String parentid,
			String siteId) {
		return artgroupdao.findChildByID(parentid,siteId);
	}
	/********************************获取文章组树的xml开始*******************************
	 * @param siteId 
	 * @param  */
	
	public String createDHTMLXML(String parentid,boolean firstFlag, String siteId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if (parentid.equalsIgnoreCase(Artgroup.ARTGROUPROOT) || firstFlag) {
			Artgroup rootGrp = artgroupdao.findById(parentid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+rootGrp.getName()+"\" id=\""+rootGrp.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + parentid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = artgroupdao.findChildByID(parentid,siteId);
		for (Map map : list) {
			String id = (String)map.get("id");
			String name = (String)map.get("name");
			buffer.append("<item text= \"" + name + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (grpIsLeaf(id,siteId)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	

	/**
	 * 判断该编码是否是叶子节点
	 * @param codeanization Id
	 * @return 
	 */
	private boolean grpIsLeaf(String id,String siteId){
		List list = artgroupdao.findChildByID(id,siteId);
		return list.size()<=0;
	}
	/********************************获取文章组树的xml结束********************************/


}



